﻿using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Data.Objects;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using FILSTARTUPS_CMS.Models;
using FILSTARTUPS_CMS.Helpers;

namespace FILSTARTUPS_CMS.Controllers
{
    [AdminAccess]
    public class UserController : Controller
    {
        FilStartUpsEntities cms_dbconnect = new FilStartUpsEntities();
        //
        // GET: /User/

        public ActionResult Index()
        {
            var user = (from u in cms_dbconnect.Users
                        join r in cms_dbconnect.Roles on u.roleidFK equals r.roleidPK
                        where u.UserStatus == true
                        select new UserGrid
                        {
                            useridPK = u.useridPK,
                            firstname = u.firstname,
                            username = u.Username,
                            lastname = u.lastname,
                            rolename = r.Rolename
                        }).ToList();
            ViewData["role"] = cms_dbconnect.Roles.OrderBy(r => r.roleidPK).ToList();

            return View(user);
        }

        public ActionResult Create()
        {
            ViewData["roleidPK"] = cms_dbconnect.Roles.OrderBy(r => r.roleidPK).ToList();
            return View();
        }

        [HttpPost]
        public ActionResult Create(User user, FormCollection form)
        {
            try
            {

                string username = form["Username"].Trim();
                var _user = cms_dbconnect.Users.Where(u => u.Username == username).SingleOrDefault();
                if (form["Password"].Trim().Length < 8 || form["Username"].Trim().Length < 6)
                {
                    TempData["message"] = string.Format("Minimum of 8 characters are required in password");
                    ViewData["roleidPK"] = cms_dbconnect.Roles.OrderBy(r => r.roleidPK).ToList();
                    return View();
                }
                if (ModelState.IsValid)
                {
                    if (_user != null)
                    {
                        //TODO ERROR MESSAGE POP-UP
                        return null;
                    }
                    else
                    {
                        user.Password = Helpers.Session_Helper.HashPassword(form["Password"].Trim());
                        user.roleidFK = Convert.ToInt32(form["roleidPK"].Trim());
                        user.Date_Join = DateTime.Now;
                        user.UserStatus = true;
                        cms_dbconnect.Users.Add(user);
                        cms_dbconnect.SaveChanges();
                        TempData["message"] = string.Format("{0} has been saved", user.Username);
                        return RedirectToAction("Index");

                    }
                }
            }
            catch
            {

            }
            return View();
        }
        public ActionResult Edit(int id)
        {
            User user = cms_dbconnect.Users.Find(id);
            var role = cms_dbconnect.Roles.OrderBy(r => r.roleidPK).ToList();
              ViewData["roleidPK"] = new SelectList(role, "roleidPK", "rolename", user.roleidFK);

            return View(user);
        }

        [HttpPost]
        public ActionResult Edit(User user, FormCollection form)
        {
            try
            {
                var user_ = cms_dbconnect.Users.Where(u => u.useridPK.Equals(user.useridPK)).SingleOrDefault();
                if (ModelState.IsValid)
                {
                    user_.roleidFK = Convert.ToInt32(form["roleidPK"].Trim());
                    user_.Password = Helpers.Session_Helper.HashPassword(form["Password"].Trim());
                    cms_dbconnect.Entry(user_).State = EntityState.Modified;
                    cms_dbconnect.SaveChanges();
                    TempData["message"] = string.Format("{0} has been modified", user.Username);
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }

            }
            catch
            {
                return View();
            }
        }
        public ActionResult Delete(int id)
        {
            User user = cms_dbconnect.Users.Find(id);
            try
            {
                cms_dbconnect.Users.Remove(user);
                cms_dbconnect.SaveChanges();
                TempData["message"] = string.Format("{0} has been deleted", user.Username);
                return RedirectToAction("Index");
            }
            catch
            {

            }
            return View();
        }
        public ActionResult Details(int id)
        {
            return View();
        }



        #region PartialViewResult
        public PartialViewResult SearchUserRole(int rolename)
        {
            var userrole = (from u in cms_dbconnect.Users
                            join r in cms_dbconnect.Roles on
                            u.roleidFK equals r.roleidPK
                            where r.roleidPK.Equals(rolename)
                            select new UserGrid
                            {
                                useridPK = u.useridPK,
                                firstname = u.firstname,
                                username = u.Username,
                                lastname = u.lastname,
                                rolename = r.Rolename
                            }).ToList();
            if (userrole != null)
            {
                return PartialView("_UserList", userrole);
            }
            else
            {
                Response.Write("No User");
                return null;
            }
        }
        #endregion

    }
}
